package com.yyk.yykywd.mvvm.ui

import android.content.Intent
import android.view.View
import androidx.lifecycle.Observer
import com.bigkoo.pickerview.view.OptionsPickerView
import com.yyk.yykywd.*
import com.yyk.yykywd.base.BaseActivity
import com.yyk.yykywd.bean.BaseBean
import com.yyk.yykywd.mvvm.entity.FollowTypeBean
import com.yyk.yykywd.mvvm.view_model.BusinessFollowViewModel
import com.yyk.yykywd.util.ToAnotherActivity
import com.yyk.yykywd.util.ToastUtil
import kotlinx.android.synthetic.main.activity_business_follow.*
import kotlinx.android.synthetic.main.rl_bottom.*

class BusinessFollowActivity : BaseActivity() {
    private lateinit var viewModel: BusinessFollowViewModel
    private lateinit var showOptionsPicker: OptionsPickerView<Any>

    override fun initData() {
        viewModel.getType()
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_business_follow
    }

    override fun initView() {
        setBackArrow()
        setTitle("业务跟进")
        tv_execute.text = "保存"

        viewModel = getViewModel(this, BusinessFollowViewModel::class.java)
        with(viewModel) {
            id = intent.getStringExtra(ID)
            type = intent.getStringExtra(TYPE)
            name = intent.getStringExtra(VISITOR_NAME)
            position = intent.getStringExtra(VISITOR_POSITION)
            phone = intent.getStringExtra(VISITOR_PHONE)

            et_visit_name.setText(name)
            et_visitor_position.setText(position)
            et_visitor_phone.setText(phone)
        }
    }

    override fun setListener() {
        super.setListener()

        tv_meeting_name.setOnClickListener {
            ToAnotherActivity.toMeetingInvitationActivity(mActivity, FROM_BUSINESS_FOLLOW)
        }

        rg_attend.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.rb_agree_attend -> {
                    viewModel.inviteResult = true
                    tv_follow_situation_hint.text = "跟进情况"
                    et_follow_situation.hint = "请填写跟进情况，如院方有意合作，预计下周签订合同"
                }
                R.id.rb_not_agree_attend -> {
                    viewModel.inviteResult = false
                    tv_follow_situation_hint.text = "未参会原因"
                    et_follow_situation.hint = "请填写未参会原因"
                }
            }
        }

        viewModel.followType.observe(this, Observer<FollowTypeBean> {
            with(viewModel) {

                if (type == FROM_POTENTIAL_CUSTOMER_AGENT ||
                        type == FROM_C_CUSTOMER_AGENT ||
                        type == FROM_SIGN_CONTRACT_AGENT ||
                        type == FROM_CUSTOMER_OPEN_SEA_AGENT) {
                    tv_follow_type.text = CALL_BACK
                    cl_meeting_invitation.visibility = View.GONE
                    cl_visitor_info.visibility = View.GONE
                    showOptionsPicker = showOptionsPicker(mActivity) { options1: Int, _: Int, _: Int ->
                        followId = followTypeBean.agent_data[options1].id
                        tv_follow_type.text = agentFollowType[options1]

                        when (getTextString(tv_follow_type)) {
                            CALL_BACK, WECHAT_FOLLOW, INVITE_COMPANY, MAIL_BACK, OTHER_FOLLOW -> {
                                cl_meeting_invitation.visibility = View.GONE
                                cl_visitor_info.visibility = View.GONE
                            }
                            MEETING_INVITATION -> {
                                cl_meeting_invitation.visibility = View.VISIBLE
                                cl_visitor_info.visibility = View.GONE
                            }
                        }
                    }
                    showOptionsPicker.setPicker(agentFollowType) //三级选择器
                } else {
                    tv_follow_type.text = VISIT_AGAIN
                    cl_meeting_invitation.visibility = View.GONE
                    cl_visitor_info.visibility = View.VISIBLE
                    showOptionsPicker = showOptionsPicker(mActivity) { options1: Int, _: Int, _: Int ->
                        followId = followTypeBean.hos_data[options1].id
                        tv_follow_type.text = hospitalFollowType[options1]

                        when (getTextString(tv_follow_type)) {
                            CALL_BACK, INVITE_COMPANY, OTHER_FOLLOW -> {
                                cl_meeting_invitation.visibility = View.GONE
                                cl_visitor_info.visibility = View.GONE
                            }
                            VISIT_AGAIN -> {
                                cl_meeting_invitation.visibility = View.GONE
                                cl_visitor_info.visibility = View.VISIBLE
                            }
                            MEETING_INVITATION -> {
                                cl_meeting_invitation.visibility = View.VISIBLE
                                cl_visitor_info.visibility = View.GONE
                            }
                        }
                    }
                    showOptionsPicker.setPicker(hospitalFollowType) //三级选择器
                }
                showOptionsPicker.setSelectOptions(0, 0, 0)
            }
        })

        tv_follow_type.setOnClickListener {
            showOptionsPicker.show()
        }

        tv_execute.setOnClickListener {
            if (getTextString(tv_follow_type) == MEETING_INVITATION) {
                if (viewModel.mid.isBlank()) {
                    ToastUtil.showShort("请选择会议邀约")
                    return@setOnClickListener
                }
                if (!viewModel.inviteResult && getTextString(et_follow_situation).isBlank()) {
                    ToastUtil.showShort("请填写未参会原因")
                    return@setOnClickListener
                } else {
                    if (getTextString(et_follow_situation).isBlank()) {
                        ToastUtil.showShort("请填写跟进情况")
                        return@setOnClickListener
                    }
                }
            } else {
                if (getTextString(et_follow_situation).isBlank()) {
                    ToastUtil.showShort("请填写跟进情况")
                    return@setOnClickListener
                }

                if (getTextString(tv_follow_type) == VISIT_AGAIN) {
                    if (getTextString(et_visit_name).isBlank()) {
                        ToastUtil.showShort("请填写拜访人姓名")
                        return@setOnClickListener
                    }
                    if (getTextString(et_visitor_position).isBlank()) {
                        ToastUtil.showShort("请填写拜访人职位")
                        return@setOnClickListener
                    }
                    if (getTextString(et_visitor_phone).isBlank()) {
                        ToastUtil.showShort("请填写拜访人电话")
                        return@setOnClickListener
                    }
                }
            }

            viewModel.saveFollow(getTextString(et_follow_situation))
        }

        viewModel.base.observe(this, Observer<BaseBean> {
            ToastUtil.showShort(it.msg)
            if (it.code == 200) {
                finish()
            }
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == FOLLOW_CODE && resultCode == FOLLOW_CODE) {
            data?.let {
                viewModel.mid = it.getStringExtra(M_ID)
                tv_meeting_name.text = it.getStringExtra(M_NAME)
            }
        }
    }

}
